package com.fenghao.subject.domain.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.fenghao.subject.common.enums.IsDeletedEnum;
import com.fenghao.subject.domain.convert.SubjectCategoryConvert;
import com.fenghao.subject.domain.entity.SubjectCategoryBO;
import com.fenghao.subject.domain.service.SubjectCategoryDomainService;
import com.fenghao.subject.infra.basic.entity.SubjectCategory;
import com.fenghao.subject.infra.basic.service.SubjectCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.alibaba.fastjson.JSON.toJSONString;

/**
 * SubjectCategoryDomainServiceImpl
 */
@Service
@Slf4j
public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainService {

    @Resource
    private SubjectCategoryService subjectCategoryService;

    /**
     * 新增题目分类
     */
    public Boolean addSubjectCategory(SubjectCategoryBO subjectCategoryBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryDomainServiceImpl.add.subjectCategoryBO:{}", toJSONString(subjectCategoryBO));
        }
        SubjectCategory subjectCategory = SubjectCategoryConvert.INSTANCE
                .convertBoToCategory(subjectCategoryBO);
        subjectCategory.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
        return subjectCategoryService.insert(subjectCategory) > 0;
    }

    /**
     * 删除题目分类
     */
    @Override
    public Boolean deleteSubjectCategory(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = new SubjectCategory();
        BeanUtil.copyProperties(subjectCategoryBO, subjectCategory);
        // 更新 isDeleted 软删
        subjectCategory.setIsDeleted(IsDeletedEnum.DELETED.getCode());
        // infra层交互
        int updateSuccess = subjectCategoryService.update(subjectCategory);
        return updateSuccess > 0;

    }

    /**
     * 更新题目分类
     */
    @Override
    public Boolean updateSubjectCategory(SubjectCategoryBO subjectCategoryBO) {
        // BO转SubjectCategory
        SubjectCategory subjectCategory = SubjectCategoryConvert.INSTANCE.convertBoToCategory(subjectCategoryBO);
        // 和真实数据层交互
        int count = subjectCategoryService.update(subjectCategory);
        return count > 0;
    }

    /**
     * 查询题目分类
     */
    public List<SubjectCategoryBO> queryCategory(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = SubjectCategoryConvert
                .INSTANCE.convertBoToCategory(subjectCategoryBO);
        // 添加查询条件未删除
        subjectCategory.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
        List<SubjectCategory> subjectCategoryList = subjectCategoryService.queryCategory(subjectCategory);
        List<SubjectCategoryBO> boList =
                SubjectCategoryConvert.INSTANCE.convertCategoryListToBoList(subjectCategoryList);
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryDomainServiceImpl.queryCategory.boList:{}", toJSONString(boList));
        }
        return boList;
    }


}
